﻿using System.Windows.Forms;
using Tasks247.Data;


namespace Tasks247
{

    public partial class MainForm : Form
    {
        private readonly ITasksRepository _repository;

        private AddUpdateState _flag = AddUpdateState.Add;

        public MainForm(ITasksRepository repository)
        {
            _repository = repository;
            InitializeComponent();

            listBoxTasks.ValueMember = "Id";
            listBoxTasks.DisplayMember = "Name";

            // this will return the valuemember... in this case Id column
            //listBoxTasks.SelectedIndex

            // this will return the select item... in this case a TaskEntity object instance
            //listBoxTasks.SelectedItem

        }

        enum AddUpdateState
        {
            Add, Update
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

            listBoxTasks.DataSource = _repository.Read();

            textBoxId.DataBindings.Add("Text", listBoxTasks.DataSource, "Id");
            textBoxName.DataBindings.Add("Text", listBoxTasks.DataSource, "Name");
            textBoxNotes.DataBindings.Add("Text", listBoxTasks.DataSource, "Notes");

        }

       
        private void buttonAdd_Click(object sender, System.EventArgs e)
        {
            buttonSave.Enabled = true;
            buttonAdd.Enabled = false;

            textBoxId.Text = "";
            textBoxName.Text = "";
            textBoxNotes.Text = "";
            textBoxId.Enabled = false;

            _flag = AddUpdateState.Add;
        }

        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            buttonSave.Enabled = false;
            buttonAdd.Enabled = true;
            textBoxId.Enabled = true;

            if (_flag==AddUpdateState.Add)
            {
                textBoxId.Text = _repository.Create(textBoxName.Text, textBoxNotes.Text).ToString();                
            }
            else
            {
                
            }

            listBoxTasks.DataSource = _repository.Read();
        }

       

    }
}
